#include "widget.h"
#include "ui_widget.h"
#include "RecordListModel.h"
#include "RecordListDelegate.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    SetNavigationBar();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::SetNavigationBar()
{
    RecordListModel * m_navigateModel = new RecordListModel(this);
    RecordListDelegate * pDelegate = new RecordListDelegate(this);
    m_navigateModel->readConfig(":/rc/resource/navigation.xml");
    ui->listView->setModel(m_navigateModel);
    ui->listView->setItemDelegate(pDelegate);

    connect(ui->listView, &QListView::clicked, this, [=](const QModelIndex& index)
    {
        RecordListModel::TreeNode* pTreeNode
                = static_cast<RecordListModel::TreeNode*>(index.data(Qt::UserRole).value<void*>());
        //处理展开关闭
        m_navigateModel->Collapse(index);

        qDebug() <<pTreeNode->labelName;

    });

    QModelIndex indexFromList = m_navigateModel->index(1, 0);
    ui->listView->clicked(indexFromList);
    ui->listView->setCurrentIndex(indexFromList);
}


